package com.zkk.tankgame04;

import java.util.Random;
import java.util.Vector;

public class EnemyTank extends Tank implements Runnable {
    private final int type = 1;//敌方坦克类型
    Vector<Shot> shots = new Vector<>();
    //增加成员 EnemyTank可以得到敌人坦克的Vector
    Vector<EnemyTank> enemyTanks = new Vector<>();
    //提供Set方法 可以将MyPanel对象中 Vector<EnemyTank> enemyTanks字段Set到EnemyTank的Vector enemyTanks字段中
    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    public int getType() {
        return type;
    }

    public EnemyTank(int x, int y) {
        super(x, y);

    }

    //编写方法，判断当前的这个敌人坦克，是否和enemyTanks中的其他敌人坦克发生重叠或者碰撞
    public boolean isTouchEnemyTank(){
        //判断当前敌人（this）方向
        switch (this.getDirect()){
            case 0:
                for (int i = 0;i < enemyTanks.size();i++){
                    //Vector中取出一个敌人坦克
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较（自己和自己比定重叠）
                    if (enemyTank != this){
                        //*如果敌人坦克是上/下*
                        //它的范围是 x:  [enemyTank.getX(),enemyTank.getX() + 40]
                        //         y:  [enemyTank.getY(),enemyTank.getY() + 60]
                        //
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            //1.当前坦克（this）左上角角坐标 [this.getX(),this.getY()]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60){
                                return true;
                            }
                            //2.当前坦克（this）右上角角坐标 [this.getX(),this.getY() + 40]
                            if (this.getX() + 40>= enemyTank.getX()
                                    && this.getX() + 40<= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60){
                                return true;
                            }

                        }
                        //*如果敌人坦克是左/右*
                        //它的范围是 x:  [enemyTank.getX(),enemyTank.getX() + 60]
                        //         y:  [enemyTank.getY(),enemyTank.getY() + 40]
                        //
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            //1.当前坦克（this）左上角角坐标 [this.getX(),this.getY()]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX() <= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40){
                                return true;
                            }
                            //2.当前坦克（this）右上角角坐标 [this.getX(),this.getY() + 40]
                            if (this.getX() + 40>= enemyTank.getX()
                                    && this.getX() + 40<= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1:
                for (int i = 0;i < enemyTanks.size();i++){
                    //Vector中取出一个敌人坦克
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较（自己和自己比定重叠）
                    if (enemyTank != this){
                        //*如果敌人坦克是上/下*
                        //它的范围是 x:  [enemyTank.getX(),enemyTank.getX() + 40]
                        //         y:  [enemyTank.getY(),enemyTank.getY() + 60]
                        //
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            //1.当前坦克（this）右上角角坐标 [this.getX() + 60,this.getY()]
                            if (this.getX() + 60 >= enemyTank.getX()
                                    && this.getX() + 60<= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 60){
                                return true;
                            }
                            //2.当前坦克（this）右下角角坐标 [this.getX() + 60,this.getY() + 40]
                            if (this.getX() + 60>= enemyTank.getX()
                                    && this.getX() + 60<= enemyTank.getX() + 40
                                    && this.getY() + 40>= enemyTank.getY()
                                    && this.getY() + 40<= enemyTank.getY() + 60){
                                return true;
                            }

                        }
                        //*如果敌人坦克是左/右*
                        //它的范围是 x:  [enemyTank.getX(),enemyTank.getX() + 60]
                        //         y:  [enemyTank.getY(),enemyTank.getY() + 40]
                        //
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            //1.当前坦克（this）右上角角坐标 [this.getX() + 60,this.getY()]
                            if (this.getX() + 60>= enemyTank.getX()
                                    && this.getX() + 60<= enemyTank.getX() + 60
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY() <= enemyTank.getY() + 40){
                                return true;
                            }
                            //2.当前坦克（this）右下角角坐标 [this.getX() + 60,this.getY() + 40]
                            if (this.getX() + 60>= enemyTank.getX()
                                    && this.getX() + 60<= enemyTank.getX() + 60
                                    && this.getY() + 40>= enemyTank.getY()
                                    && this.getY() + 40<= enemyTank.getY() + 40){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2:
                for (int i = 0;i < enemyTanks.size();i++){
                    //Vector中取出一个敌人坦克
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较（自己和自己比定重叠）
                    if (enemyTank != this){
                        //*如果敌人坦克是上/下*
                        //它的范围是 x:  [enemyTank.getX(),enemyTank.getX() + 40]
                        //         y:  [enemyTank.getY(),enemyTank.getY() + 60]
                        //
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            //1.当前坦克（this）左下角角坐标 [this.getX(),this.getY() + 60]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX()<= enemyTank.getX() + 40
                                    && this.getY() + 60>= enemyTank.getY()
                                    && this.getY() + 60<= enemyTank.getY() + 60){
                                return true;
                            }
                            //2.当前坦克（this）右下角角坐标 [this.getX() + 40,this.getY() + 60]
                            if (this.getX() + 40>= enemyTank.getX()
                                    && this.getX() + 40<= enemyTank.getX() + 40
                                    && this.getY() + 60>= enemyTank.getY()
                                    && this.getY() + 60<= enemyTank.getY() + 60){
                                return true;
                            }

                        }
                        //*如果敌人坦克是左/右*
                        //它的范围是 x:  [enemyTank.getX(),enemyTank.getX() + 60]
                        //         y:  [enemyTank.getY(),enemyTank.getY() + 40]
                        //
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            //1.当前坦克（this）左下角角坐标 [this.getX(),this.getY() + 60]
                            if (this.getX()>= enemyTank.getX()
                                    && this.getX()<= enemyTank.getX() + 60
                                    && this.getY() + 60>= enemyTank.getY()
                                    && this.getY() + 60<= enemyTank.getY() + 40){
                                return true;
                            }
                            //2.当前坦克（this）右下角角坐标 [this.getX() + 40,this.getY() + 60]
                            if (this.getX() + 40>= enemyTank.getX()
                                    && this.getX() + 40<= enemyTank.getX() + 60
                                    && this.getY() + 60>= enemyTank.getY()
                                    && this.getY() + 60<= enemyTank.getY() + 40){
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3:
                for (int i = 0;i < enemyTanks.size();i++){
                    //Vector中取出一个敌人坦克
                    EnemyTank enemyTank = enemyTanks.get(i);
                    //不和自己比较（自己和自己比定重叠）
                    if (enemyTank != this){
                        //*如果敌人坦克是上/下*
                        //它的范围是 x:  [enemyTank.getX(),enemyTank.getX() + 40]
                        //         y:  [enemyTank.getY(),enemyTank.getY() + 60]
                        //
                        if (enemyTank.getDirect() == 0 || enemyTank.getDirect() == 2){
                            //1.当前坦克（this）左上角角坐标 [this.getX(),this.getY()]
                            if (this.getX() >= enemyTank.getX()
                                    && this.getX()<= enemyTank.getX() + 40
                                    && this.getY() >= enemyTank.getY()
                                    && this.getY()<= enemyTank.getY() + 60){
                                return true;
                            }
                            //2.当前坦克（this）左下角角坐标 [this.getX(),this.getY() + 40]
                            if (this.getX()>= enemyTank.getX()
                                    && this.getX()<= enemyTank.getX() + 40
                                    && this.getY() + 40>= enemyTank.getY()
                                    && this.getY() + 40<= enemyTank.getY() + 60){
                                return true;
                            }

                        }
                        //*如果敌人坦克是左/右*
                        //它的范围是 x:  [enemyTank.getX(),enemyTank.getX() + 60]
                        //         y:  [enemyTank.getY(),enemyTank.getY() + 40]
                        //
                        if (enemyTank.getDirect() == 1 || enemyTank.getDirect() == 3){
                            //1.当前坦克（this）左上角角坐标 [this.getX(),this.getY() ]
                            if (this.getX()>= enemyTank.getX()
                                    && this.getX()<= enemyTank.getX() + 60
                                    && this.getY()>= enemyTank.getY()
                                    && this.getY()<= enemyTank.getY() + 40){
                                return true;
                            }
                            //2.当前坦克（this）左下角角坐标 [this.getX(),this.getY() + 40]
                            if (this.getX()>= enemyTank.getX()
                                    && this.getX()<= enemyTank.getX() + 60
                                    && this.getY() + 40>= enemyTank.getY()
                                    && this.getY() + 40<= enemyTank.getY() + 40){
                                return true;
                            }
                        }
                    }
                }
                break;
        }
        return false;
    }



    @Override
    public void run() {
        while (true) {
            //这里我们判断如果shots size()=0,创建一颗子弹，放入到shots集合 并启动子弹线程
            if (isLive && shots.size() < 2) {
                Shot s = null;

                //判断坦克的方向，创建对应的子弹
                switch (getDirect()) {
                    case 0:
                        s = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        s = new Shot(getX() + 60, getY() + 20, 1);
                    case 2:
                        s = new Shot(getX() + 20, getY() + 60, 2);
                        break;
                    case 3:
                        s = new Shot(getX(), getY() + 20, 3);
                        break;
                }
                shots.add(s);
                //启动子弹线程
                new Thread(s).start();
            }

            //根据坦克方向来移动
            switch (getDirect()) {
                case 0:
                    for (int i = 0; i < 30; i++) {
                        if (getX() > 0 && !isTouchEnemyTank())
                        moveUp();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 30; i++) {
                        if (getX() + 60 < 1000 && !isTouchEnemyTank())
                        moveRight();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        if (getY() + 60 < 750 && !isTouchEnemyTank())
                        moveDown();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        if (getX() > 0 && !isTouchEnemyTank())
                        moveLeft();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    break;
            }

            //随机改变方向
            setDirect((int) (Math.random() * 4));
            //考虑线程退出时机
            if (!isLive) {
                break;
            }
        }
    }
}
